﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using models;
using Mvc.JQuery.Datatables;

namespace MvcApplication3.Controllers
{
    public class LogoController : Controller
    {
        private FoulekModelContainer db = new FoulekModelContainer();

        //
        // GET: /Logo/

        public ActionResult Index()
        {
            var questions = db.Questions.Include(q => q.Category);
            return View(questions.ToList());
        }


        public ActionResult Rate()
        {
            var questions = db.Questions.OfType<Logo>().Where(question => question.Weight == 0).Include(question => question.Category);
            var randomLogo = questions.OrderBy(logo => Guid.NewGuid()).Take(1).Single();
            return View(randomLogo);
        }



        [HttpPost]
        public ActionResult Rate(Logo model)
        {
            if (model.Weight > 0)
            {
                TempData["Message"]= string.Format("Logo {0} upvoted.",model.Name);
            }
            else
            {
                TempData["Message"] = string.Format("Logo {0} downvoted.", model.Name); 
            }

            var currentLogo = db.Questions.OfType<Logo>().Where(p => p.Id == model.Id).Single();
            //currentLogo.Answer = model.Answer;
            currentLogo.Weight = model.Weight;
            db.SaveChanges();
            ModelState.Clear();
            var questions = db.Questions.OfType<Logo>().Where(question => question.Weight == 0).Include(question => question.Category);
            var randomLogo = questions.OrderBy(logo => Guid.NewGuid()).Take(1).Single();
            return PartialView("QuickRate", randomLogo);
        }

        //
        // GET: /Logo/Details/5

        public ActionResult Details(int id = 0)
        {
            Question question = db.Questions.Find(id);
            if (question == null)
            {
                return HttpNotFound();
            }
            return View(question);
        }

        //
        // GET: /Logo/Create

        public ActionResult Create()
        {
            ViewBag.CategoryId = new SelectList(db.Categories, "Id", "Name");
            return View();
        }

        //
        // POST: /Logo/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Question question)
        {
            if (ModelState.IsValid)
            {
                db.Questions.Add(question);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.CategoryId = new SelectList(db.Categories, "Id", "Name", question.CategoryId);
            return View(question);
        }

        //
        // GET: /Logo/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Question question = db.Questions.Find(id);
            if (question == null)
            {
                return HttpNotFound();
            }
            ViewBag.CategoryId = new SelectList(db.Categories, "Id", "Name", question.CategoryId);
            return View(question);
        }

        //
        // POST: /Logo/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Question question)
        {
            if (ModelState.IsValid)
            {
                db.Entry(question).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.CategoryId = new SelectList(db.Categories, "Id", "Name", question.CategoryId);
            return View(question);
        }

        //
        // GET: /Logo/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Question question = db.Questions.Find(id);
            if (question == null)
            {
                return HttpNotFound();
            }
            return View(question);
        }

        //
        // POST: /Logo/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Question question = db.Questions.Find(id);
            db.Questions.Remove(question);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}